package com.song.listener;

import com.song.domain.Orders;
import com.song.domain.Product;
import com.song.mapper.OrdersMapper;
import com.song.service.impl.OrdersServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author TonySong
 * @date 2024/6/17 0017
 * @time 10:47
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class OrdersListener {

      private final OrdersMapper ordersMapper;

       @RabbitListener(bindings = {@QueueBinding(
                value = @Queue(value = "orders", durable = "true"),
                exchange = @Exchange(value = "topic-exchange", ignoreDeclarationExceptions = "true", type = "topic"),
                key = "orders.buy"
       )
       })
       public void listen(Product product) {
            log.info("received product: {}", product);
            //执行业务代码

           Orders orders =new Orders();
           orders.setPid(product.getId());
           System.err.println("得到商品ID");
           ordersMapper.insert(orders);
           System.err.println("订单创建成功");
           //修改库存
       }
}
